{extends file="index.tpl"}
{block name=head_title}ĐỊA CHỈ{/block}
{block name=head_script}
    <script>

    </script>
    {if $action == "edit"}
        <script>
            $(document).ready(function() {
                $('#thanhpho').val({$diachi[0].MaTP});
                getQH($("#thanhpho").val());
            });
        </script>
    {/if}
{/block}
{block name=head_style}{/block}
{block name=body}
    {if isset($action)}
        {if $action == "view"}
            <a href="/diachi/add" class="link_add_new">Thêm địa chỉ</a>
            <br />
            <br />
            <!-- Table View -->
            <table cellspacing="0" cellpadding="3" align="center" border="1" class="table">
                <thead>
                    <tr class="table_thead_tr">
                        <th colspan="7">
                            DANH SÁCH ĐỊA CHỈ
                        </th>
                    </tr>
                    <tr>
                        <th style="width: 40px">No.</th>
                        <th style="width: 250px">Address</th>
                        <th style="width: 200px">Ward</th>
                        <th style="width: 150px">District</th>
                        <th style="width: 100px">City</th>
                        <th style="width: 50px">Edit</th>
                        <th style="width: 60px">Delete</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach from=$diachi item=i}
                        <tr>
                            <td style="text-align: center">{$i@index + 1}</td>         
                            <td style="text-align: center">{$i.DiaChi}</td>
                            <td style="text-align: center">{$i.TenPX}</td>
                            <td style="text-align: center">{$i.TenQH}</td>
                            <td style="text-align: center">{$i.TenTP}</td>  
                            <td style="text-align: center"><a href="/diachi/edit/{$i.MaDC}">Edit</a></td>
                            <td style="text-align: center"><a href="/diachi/delete/{$i.MaDC}" onclick="return confirm('Are you sure delete No.{$i.MaDC}?');">Delete</a></td>
                        </tr>
                    {/foreach}
                </tbody>
            </table>
        {/if}
        {if $action == "add" || $action == "edit"}
            <!-- Table Add/Edit -->
            <form id="frmAddress" onsubmit="return check_address(this);" action="/diachi" method="post">
                <table cellspacing="0" cellpadding="3" align="center" border="1" class="table">
                    <thead>
                        <tr class="table_thead_tr">
                            <th colspan="2">
                                {if $action == "add"}
                                    {"Thêm địa chỉ"|upper}
                                {/if}
                                {if $action == "edit"}
                                    {"Sửa địa chỉ"|upper}
                                {/if}
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr style="color:#000066;">
                            <td>Thành Phố</td>
                            <td>
                                <select id="thanhpho" name="thanhpho" onchange="getQH(this.value);" style="width: 200px">
                                    {if isset($thanhpho)}
                                        <option value="0">Chọn</option>
                                        {foreach from=$thanhpho item=i}
                                            <option value="{$i.MaTP}">{$i.TenTP}</option>
                                        {/foreach}
                                    {/if}
                                </select>

                            </td>
                        </tr>
                        <tr>
                            <td>Quận/Huyện</td>
                            <td> 
                                <select id="quanhuyen" name="quanhuyen" onchange="getPX(this.value);" style="width: 200px">
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Phường/Xã</td>
                            <td>
                                <select id="phuongxa" name="phuongxa" style="width: 200px">
                                </select> 
                            </td>
                        </tr>
                        <tr>
                            <td>Địa chỉ</td>
                            <td>  
                                <input id="address" name="address" type="text" style="width: 200px">
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align: center">  
                                <input type="submit" id="submit" name="submit" value="{if $action == "add"}Add{else}Update{/if}" />
                            </td>
                        </tr>
                        {if $action == "edit"}
                        <input type="hidden" id="maDC" name="maDC" value="{$diachi[0].MaDC}" />
                    {/if}
                    <p id="fulladdress"></p>
                    </tbody>
                </table>
            </form>
        {/if}
    {/if}
    <!-- Hiện thông báo sau khi xử lý dữ liệu -->
    {if isset($success)}
        <span>
            {if $success==1}
                <p style="color:green">Thao tác thành công!</p>
            {/if}
            {if $success==-1}
                <p style="color:red">Thao tác thất bại vì dữ liệu bị trùng hoặc không tồn tại!</p>
            {/if}
            {if $success==0}
                <p style="color:red">Thao tác thất bại vì dữ liệu đang sử dụng!</p>
            {/if}
            <p>Đang chuyển đến trang "Danh sách địa chỉ" sau: <span id="redirect" style="color: red; font-weight: bold">3</span> giây. 
                Hoặc <a href="/diachi/view">chuyển ngay</a>
            </p>
            <script>
                setTimeout(function() {
                    $("#redirect").text('' + 2);
                }, 1000);
                setTimeout(function() {
                    $("#redirect").text('' + 1);
                }, 2000);
                setTimeout(function() {
                    $("#redirect").text('' + 0);
                }, 3000);
                setTimeout(function() {
                    top.location.href = '/diachi/view';
                }, 3500);
            </script>
        </span>
    {/if}
{/block}